//
//  RankListViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/18.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "RankListViewController.h"
#import "CustomerNetworking.h"
#import "RankListModel.h"
#import "RankDetailModel.h"
#import "RankListTableViewCell.h"
#import "RankListDetailViewController.h"
#import "AsiaNewSongViewController.h"
#import <MBProgressHUD.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)
@interface RankListViewController ()<UITableViewDataSource, UITableViewDelegate, MBProgressHUDDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataArray;

@property (nonatomic, retain) NSMutableArray *songsArray;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation RankListViewController
- (void)dealloc
{
    [_dataArray release];
    [_songsArray release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"排行榜";
    self.dataArray = [NSMutableArray array];
    self.songsArray = [NSMutableArray array];
    [self createSubViews];
    [self loadData];
}

-(void)createSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT_PRO(50)) style:UITableViewStylePlain];;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [_tableView release];
}

-(void)loadData
{
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    
    //常用的设置
    //小矩形的背景色
    // self.HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    self.HUD.labelText = @"正在进行加载~";
    
    [CustomerNetworking customerWithURL:@"http://api.songlist.ttpod.com/channels/bhb/children?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679448190071&longtitude=121.5286730322579" andParamer:nil andBlock:^(id response) {
        for (NSDictionary *dic in response) {
            RankListModel *rankListModel = [RankListModel modelWithDictionary:dic];
            [self.dataArray addObject:rankListModel];
            for (NSDictionary *dic in rankListModel.refs) {
                
                RankDetailModel *rankDetailModel = [RankDetailModel modelWithDictionary:dic];
                [self.songsArray addObject:rankDetailModel];
            }
        }
        NSLog(@"%ld", self.songsArray.count);
        [self.tableView reloadData];
        [self.HUD hide:YES];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    RankListModel *rankListModel = _dataArray[section];
    return rankListModel.refs.count;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH / 3 + HEIGHT_PRO(10);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT_PRO(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    RankListModel *rankListModel = _dataArray[section];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_PRO(50))];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), WIDTH_PRO(100), HEIGHT_PRO(20))];
    headerTitle.backgroundColor = [UIColor clearColor];
    headerTitle.shadowColor = [UIColor whiteColor];
    [headerTitle setShadowOffset:CGSizeMake(0, 1)];
    headerTitle.text = rankListModel.name;
    [headerView addSubview:headerTitle];
    return headerView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        RankDetailModel *rankDetailModel = _songsArray[indexPath.row];
        RankListTableViewCell *cell = [RankListTableViewCell rankListTableViewCellWith:tableView];
        cell.rankDetailModel = rankDetailModel;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else if (indexPath.section == 1){
        RankDetailModel *rankDetailModel = _songsArray[indexPath.row + 7];
        RankListTableViewCell *cell = [RankListTableViewCell rankListTableViewCellWith:tableView];
        cell.rankDetailModel = rankDetailModel;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else{
        RankDetailModel *rankDetailModel = _songsArray[indexPath.row + 11];
        RankListTableViewCell *cell = [RankListTableViewCell rankListTableViewCellWith:tableView];
        cell.rankDetailModel = rankDetailModel;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        AsiaNewSongViewController *asiaNewSong = [[AsiaNewSongViewController alloc] init];
        [self.navigationController pushViewController:asiaNewSong animated:YES];
        [asiaNewSong release];
        
    }else{
    RankListModel *rankListModel = _dataArray[indexPath.section];
    RankDetailModel *rankDetailModel = [RankDetailModel modelWithDictionary:rankListModel.refs[indexPath.row]];
    RankListDetailViewController *rankListDetail = [[RankListDetailViewController alloc] init];
    rankListDetail.rankID = rankDetailModel.ranklist_id;
    [self.navigationController pushViewController:rankListDetail animated:YES];
    [rankListDetail release];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
